/**
 * User: yd
 * Date: 2019/5/27
 * Time: 9:48
 **/
//axios封装
import router from '../router';
import loadaxios from 'axios'
import {Loading, Message} from 'element-ui'
let loading;
function showMessage(txt,type){
    Message({
        showClose: true,
        message: txt,
        type: type
    })
}

// axios 配置
loadaxios.defaults.timeout = 5000;

// http request 拦截器
loadaxios.interceptors.request.use(
    config => {
        loading = Loading.service({fullscreen: true});
        if (localStorage.getItem('blogToken')) {
            config.headers.authorization = localStorage.getItem('blogToken');
        }
        return config;
    },
    err => {
        loading.close();
        showMessage('请求失败','warning');
        return Promise.reject(err);
    });

let code = {
    SUCCESS: 200,         //success
    REDIRECT: 304,        //redirect
    UN_LOGIN: 10001,      //没有登录
    BIND_PHONE: 10002,     //没有绑定手机,
    MESSAGE: 10003,          //消息提醒
    LOGIN_EXPIRE: 10004     //登录过期
}

// http response 拦截器
loadaxios.interceptors.response.use(
    res=> {
        if(res.headers.authorization){
            localStorage.setItem('blogToken',res.headers.authorization);
        }
        loading.close();
        if(res.status === 200){
            let { code:statusCode,message } = res.data;
            if(statusCode === code.SUCCESS){
                return res.data;
            }else if(statusCode === code.MESSAGE){ //10003提示信息
                showMessage(message,'warning');
                return Promise.reject(res.data);
            }else if(statusCode == code.UN_LOGIN){//10001 跳登录
                router.push('/login');
                return Promise.reject(res.data);
            }
        }
    },
    err => {
        loading.close();
        if (err && err.response) {
            switch (err.response.status) {
                case 400:
                    showMessage('请求错误','error');
                    break
                case 401:
                    showMessage('未授权，请登录','warning');
                    break
                case 403:
                    showMessage('拒绝访问','warning');
                    break
                case 404:
                    showMessage(`请求地址出错: ${err.response.config.url}`,'error');
                    break
                case 408:
                    showMessage('请求超时','warning');
                    break
                case 500:
                    showMessage('服务器内部错误','error');
                    break
                case 501:
                    showMessage('服务未实现','error');
                    break
                case 502:
                    showMessage('网关错误','error');
                    break
                case 503:
                    showMessage('服务不可用','warning');
                    break
                case 504:
                    showMessage('网关超时','warning');
                    break
                case 505:
                    showMessage('HTTP版本不受支持','warning');
                    break
                default:
            }
        }
        return Promise.reject(err)
    });

export default loadaxios;